package com.jeasonzhao.web;

import javax.servlet.jsp.PageContext;
import com.jeasonzhao.commons.utils.Algorithms;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class WebContextWrapper implements IWebContextWrapper
{
    private IHtmlWriter writer = null;
    private PageContext pageContext = null;
    public WebContextWrapper(PageContext context,IHtmlWriter writer)
    {
        this.pageContext = context;
        this.writer = writer;
    }
    public WebContextWrapper(IHtmlWriter writer)
        {
            this.writer = writer;
    }
    public WebContextWrapper(PageContext context)
    {
        this.pageContext = context;
        this.writer = new WebHtmlWriter(null,context,false);
    }

    public WebContextWrapper(BodyTagSupport tag,PageContext context,boolean isUsingBodyWriter)
    {
        this.pageContext = context;
        this.writer = new WebHtmlWriter(tag,context,isUsingBodyWriter);
    }

    public Object get(String key)
    {
        if(Algorithms.isEmpty(key) || null == pageContext)
        {
            return null;
        }
        return ServletHelper.getParameter(pageContext.getRequest(),key);
    }

    public IHtmlWriter getWriter()
    {
        return this.writer;
    }

    public javax.servlet.http.HttpServletRequest getHttpServletRequest()
    {
        return null == this.pageContext
            ? null
            : (javax.servlet.http.HttpServletRequest) pageContext.getRequest();
    }

    public javax.servlet.http.HttpServletResponse getHttpServletResponse()
    {
        return null == pageContext
            ? null
            : (javax.servlet.http.HttpServletResponse) pageContext.getResponse();
    }

}
